/*
********************************************************************************
Description:	Master file that sets up all the data and runs all the analysis
********************************************************************************
*/

version 12.1
set more off
set linesize 255
set matsize 2000

* Paths
*******
run "S:\Projects\YOUR PROJECT NAME\Do\paths.do"
adopath ++ "${dofiles}"


*master do-file log
log using "${logfolder}\master.smcl", replace name(MasterDoFile)


*******************
* Set up CQI data *
*******************

* Import and clean
run "${dofiles}\CQI_cleanEnquiriesData.do"
cleanCQIEnquiries, saving("${CQIoutdata}\CQI_enquiry_incometax.dta")

**************************
* Set up Valid View data *
**************************

* Import, clean and append
* This should create the final VV/IV dataset

* log using "${logfolder}\creating_VVIV.smcl", replace name(creating_VVIV)
run "${dofiles}\VV_cleanData.do"
cleanVV, saving("${VVoutdata}\ValidView_1997_2012.dta")
* log close creating_VVIV


*********************
* Set up SA302 data *
*********************

* Import, clean and append
* This should create the final SA302 dataset 

*log using "${logfolder}\creating_SA302.smcl", replace name(creating_SA302)
run "${dofiles}\SA302_cleanData.do"
cleanSA302, saving("${SA302outdata}\SA302_1997_2012.dta")
*log close creating_SA302



****************
* Descriptives *
****************

* CQI and VV/IV data
********************

* This file creates some of the audit descriptives (number of audits and audit lags)
*	Specifically Table 2, Figure 1, Table A1
do "${dofiles}\CV_buildInitialDescriptives.do"



****************************************************************
* CONTROL GROUP APPROACH 1: COULD HAVE BEEN AUDITED BUT WASN'T *
****************************************************************

* Create analysis dataset
*************************

run "${dofiles}\CVS_constructCouldHaveAnalysisDataset.do"
constructCouldHaveDatset, filename(CouldhaveSample)


* Create secondary analysis dataset
***********************************
*  Different control group to above: stratified random sample

run "${dofiles}\CVS_constructMatchedCouldHaveDataset.do"
constructMatchedCouldHaveDataset, filename(CouldhaveSample_matchedOnSurvives) 

	
* CQI, Valid View and SA302 data descriptives
*********************************************

run "${dofiles}\CVS_buildInitialDescriptives.do"
* Table 1: Table with proportion of individuals with positive amounts of each income component
tableOfPropWithEachIncComponent using "$mergedata\CouldhaveSample.dta"
* Table 4: Autocorrelation by income source
persistanceOfIncomeSource using "$mergedata\CouldhaveSample.dta"
* Figure A1: Non-compliance by prior income quintile
tableOfAuditSubgroupOutcomes using "$mergedata\CouldhaveSample.dta"

	
* Check balancing
******************
run "${dofiles}\CVS_couldHaveBalancingTest.do"

*Table 3: sample balance conditional on filing history
runBalancingTests cpi using "$mergedata\CouldhaveSample.dta", saving("${results}\Table3.csv") trimlevel(1) ctrlsurvives(4)

*Table A2: sample balance, unconditional
runBalancingTests cpi using "$mergedata\CouldhaveSample.dta", saving("${results}\TableA2.csv") trimlevel(1) ctrlsurvives(0)



* Run main results
******************
run "${dofiles}\CVS_couldHaveRegressions.do"
*Figures 2 and 3: Dynamic effects on total tax and total income
couldHaveResults using "$mergedata\CouldhaveSample.dta"
*Figure 4: Relative dynamics by income source
couldHaveResults_byIncSource using "$mergedata\CouldhaveSample.dta"


*Figure A2: Dynamic effects after reweighting
**********************************************
do "${dofiles}\CVS_reweightedDynamics.do"



***********************************************
* CONTROL GROUP APPROACH 2: AUDITED IN FUTURE *
***********************************************

*Table 5: impact by audit outcome
do "${dofiles}\CVS_forwardLookingRegressions.do"


************
*APPENDIX C*
************

do "${dofiles}\AppendixC.do"



* Close master log file
log close MasterDoFile

